package org.csu.measure.service;

import org.csu.measure.Analyze.XMLParse;
import org.csu.measure.domain.*;
import org.csu.measure.domain.Class;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GetData {

    private static List<Class> classList; //存储类列表
    private static List<Association> associationList;  //存储类的关联关系
    private static List<Dependency> dependencyList;  //存储类的依赖关系
    private static List<Generalization> generalizationList; //存储类的继承关系

    public static List<Class> getClassList() {
        return classList;
    }

    public static List<Association> getAssociationList() {
        return associationList;
    }

    public static List<Dependency> getDependencyList() {
        return dependencyList;
    }

    public Class getClassByName(String name, List<Class> classList){
        for (Class c:classList
             ) {
            if(name == c.getName()){
                return c;
            }
        }
        return null;
    }

    public List<Class> getData() {
        try {
            classList = new ArrayList<>();
            associationList = new ArrayList<>();
            dependencyList = new ArrayList<>();
            generalizationList = new ArrayList<>();

            System.out.println("----------START ANALYSIS----------");
            // 创建SAXReader
            SAXReader reader = new SAXReader();
            // 读取xml文件
            Document document = reader.read(new File("test.xml"));
            // 获取根节点
            Element node1 = document.getRootElement();

            //获取类的节点
            Element classElement = node1.element("Classes");
            List<Element> classlist = classElement.elements("Class");

            for (Element node : classlist) {
            List<Element> classInfo = node.elements();

                Class targetClass = new Class();
                targetClass.setId(node.attributeValue("Id"));//存储目标类
                XMLParse.analyzeClass(classInfo, targetClass);
                classList.add(targetClass);
            }


            try {
                //获取依赖关系的节点
                Element dependencyElement = node1.element("Dependencies");

                if(dependencyElement.element("Dependency") != null){
                    List<Element> dependencylist = dependencyElement.elements("Dependency");

                    for (Element node : dependencylist) {
                        List<Element> dependencyInfo = node.elements();
                        Dependency dependency = new Dependency();
                        XMLParse.analyzeDependency(dependencyInfo, classList, dependency);
                        dependencyList.add(dependency);
                    }
                }


            }catch (NullPointerException e){
                e.printStackTrace();
            }

            try {

                //获取依赖关系的节点
                Element associationElement = node1.element("Associations");
                if(associationElement.element("Association") != null){
                    List<Element> associationlist = associationElement.elements("Association");

                    for (Element node : associationlist) {
                        List<Element> associationInfo = node.elements();
                        Association association = new Association();
                        XMLParse.analyzeAssociation(associationInfo, classList, association);
                        associationList.add(association);
                    }
                }


            }catch (NullPointerException e){
                e.printStackTrace();
            }

            try {
                //获取继承关系的节点
                Element generalizationElement = node1.element("Generalizations");
                if(generalizationElement.element("Generalization") != null){
                    List<Element> generalizationlist = generalizationElement.elements("Generalization");

                    for (Element node : generalizationlist) {
                        List<Element> generalizationInfo = node.elements();
                        Generalization generalization = new Generalization();
                        XMLParse.analyzeGeneralization(generalizationInfo, classList, generalization);
                        generalizationList.add(generalization);
                    }

                }


            }catch (NullPointerException e){
                e.printStackTrace();
            }


            for (Class c:classList
                 ) {

                //将关联关系绑定到类中
                for (Association a:associationList
                     ) {
                    if(a.getObject().equals(c.getName())){
                        c.addAssociations(a.getRelatedObject());
                    }
                    if (a.getRelatedObject().equals(c.getName())){
                        c.addAssociations(a.getObject());
                    }
                }

                //将依赖关系绑定到类中
                for (Dependency d:dependencyList
                ) {
                    if(d.getDependent().equals(c.getName())){
                        c.addDependencies(d.getOwn());
                    }
                    if (d.getOwn().equals(c.getName())){
                        c.addDependencies(d.getDependent());
                    }
                }

                //将继承关系绑定到类中
                for (Generalization g:generalizationList
                ) {
                    if(g.getParent().equals(c.getName())){
                        c.addChildren(getClassByName(g.getSubclass(),classList));
                    }
                    if(g.getSubclass().equals(c.getName())){
                        c.setFather(getClassByName(g.getParent(),classList));
                    }
                }
            }


            for (int i = 0; i < classList.size(); i++) {
                Class s = classList.get(i);
                CKService ck = new CKService(s, classList);
                LKService lk = new LKService(s, classList);
                CKMetrics c = ck.getCk(s);

                LKMetrics l = lk.getLk(s);

                s.setCk(c);
                s.setLk(l);

                System.out.println("类名：" + s.getName() + "   ID：" + s.getId());

                for (int a = 0; a < s.getAttributes().size(); a++) {
                    AttributeOfClass attribute = s.getAttributes().get(a);
                    System.out.println("属性" + (a + 1) + "---->  类属性名：" + attribute.getName() + "   类属性可见性：" + attribute.getVisibility());
                }

                for (int o = 0; o < s.getOperations().size(); o++) {
                    Operation operation = s.getOperations().get(o);
                    System.out.println("方法" + (o + 1) + "---->  类方法名：" + operation.getName() + "   类方法可见性：" + operation.getVisibility());
                }

                for (int j = 0; j < classList.size(); j++) {
                    Class t = classList.get(j);
                    if (t.getChildren() != null) {
                        for (Class child:t.getChildren()
                             ) {
                            System.out.println("子类：" + child.getName());
                        }
                    }
                }



            }

            System.out.println("----------END ANALYSIS----------");
            return classList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
